//
//  ReportDeviceAlarmCell.m
//  efairy
//  设备报警统计列表
//  Created by 李锋 on 2019/3/14.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportDeviceAlarmCell.h"
#import "ReportAlarmPointListCell.h"
#import "ReportDeviceAlarmModel.h"

@interface ReportDeviceAlarmCell()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray<DeviceStatisticsModel *> *dataArray;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic,strong) NSMutableArray<UIView *> *viewArray;
@property (nonatomic,strong) NSMutableArray<NSNumber *> *heightArray;
@property (nonatomic,assign)CGFloat tableviewH;

@end

@implementation ReportDeviceAlarmCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        _viewArray = [[NSMutableArray alloc]init];
//        _labelArrays = [[NSMutableArray alloc]init];
        _heightArray  = [[NSMutableArray alloc]init];
        [self initView];
    }
    return self;
}

//定义cell的宽度
- (void)setFrame:(CGRect)frame {
    frame.origin.x += 8;//8 为margin 值
    frame.size.width -= 2 * 8;
//    frame.size.height = _dataArray.count*34+10;
    [super setFrame:frame];
}


-(void)initView{
    _deviceName = [[UILabel alloc]init];//设备名字
    _deviceName.textColor = ColorFromRGB(0x333333);
    _deviceName.font = [UIFont boldSystemFontOfSize:16.f];
    
    _score = [[UILabel alloc]init];//评分
    _score.textColor = ColorFromRGB(0xFF7200 );
    _score.font = [UIFont systemFontOfSize:14.f];
    _score.textAlignment = NSTextAlignmentRight;
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    _personLiableTitle = [[UILabel alloc]init];//项目负责人
    _personLiableTitle.textColor = ColorFromRGB(0x333333);
    _personLiableTitle.font = [UIFont systemFontOfSize:14.f];
    _personLiableTitle.text = @"项目负责人";
   
    _personLiable = [[UILabel alloc]init];//项目负责人
    _personLiable.textColor = ColorFromRGB(0x666666);
    _personLiable.font = [UIFont systemFontOfSize:14.f];
    _personLiable.textAlignment = NSTextAlignmentRight;
    
    _phoneImage = [[UIImageView alloc]init];
    _phoneImage.image = [UIImage imageNamed:@"icon_call_phone"];
    _phoneImage.userInteractionEnabled =YES;
    _phoneImage.exclusiveTouch = YES;
    UITapGestureRecognizer *callPhone = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickPhoneImage:)];
    [_phoneImage addGestureRecognizer:callPhone];
    
    _titleView = [[UIView alloc]init];
    _titleView.backgroundColor  = ColorFromRGB(0xf9f9fb);
    
    
    
    
    _deviceType = [[UILabel alloc]init];//设备类型
    _deviceType.textColor = ColorFromRGB(0x333333);
    _deviceType.font = [UIFont systemFontOfSize:12.f];
    _deviceType.text = @"设备类型";
    _deviceType.textAlignment = NSTextAlignmentCenter;
    
    _rows = [[UILabel alloc]init];//点位总数
    _rows.textColor = ColorFromRGB(0x333333);
    _rows.font = [UIFont systemFontOfSize:12.f];
    _rows.text = @"点位";
    _rows.textAlignment = NSTextAlignmentCenter;
    
    _alarmRows = [[UILabel alloc]init];//报警点位数
    _alarmRows.textColor = ColorFromRGB(0x333333);
    _alarmRows.font = [UIFont systemFontOfSize:12.f];
    _alarmRows.text = @"报警";
    _alarmRows.textAlignment = NSTextAlignmentCenter;
    
    _waringRows = [[UILabel alloc]init];//预警点位数
    _waringRows.textColor = ColorFromRGB(0x333333);
    _waringRows.font = [UIFont systemFontOfSize:12.f];
    _waringRows.text = @"预警";
    _waringRows.textAlignment = NSTextAlignmentCenter;
    
    _troubleRows = [[UILabel alloc]init];;//故障点位
    _troubleRows.textColor = ColorFromRGB(0x333333);
    _troubleRows.font = [UIFont systemFontOfSize:12.f];
    _troubleRows.text = @"故障";
    _troubleRows.textAlignment = NSTextAlignmentCenter;
    [_titleView sd_addSubviews:@[_deviceType,_rows,_alarmRows,_waringRows,_troubleRows]];
     _troubleRows.sd_layout.leftSpaceToView(_waringRows, 0).heightIs(34).widthIs(48).rightSpaceToView(_titleView, 0);
    _waringRows.sd_layout.leftSpaceToView(_alarmRows, 0).heightIs(34).widthIs(48).rightSpaceToView(_troubleRows, 0);
     _alarmRows.sd_layout.leftSpaceToView(_rows, 0).heightIs(34).widthIs(48).rightSpaceToView(_waringRows, 0);
     _rows.sd_layout.leftSpaceToView(_deviceType, 0).heightIs(34).widthIs(48).rightSpaceToView(_alarmRows, 0);
    _deviceType.sd_layout.heightIs(34).leftSpaceToView(_titleView, 8).rightSpaceToView(_rows, 0);
   
   
    _tableView = [[UITableView alloc]init];
//    _tableView.backgroundColor = ColorFromRGB(0xf2f2f4);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];//多余的分割线不显示


    _tableUIView = [[UIView alloc]init];
    
    [self.contentView sd_addSubviews:@[_deviceName,_score,line,_personLiableTitle,_personLiable,_phoneImage,_titleView,_tableView]];
    _deviceName.sd_layout.heightIs(16).leftSpaceToView(self.contentView, 12).rightSpaceToView(self.contentView, 62)
    .topSpaceToView(self.contentView, 12);
    _score.sd_layout.heightIs(14).widthIs(60).rightSpaceToView(self.contentView, 16).topSpaceToView(self.contentView, 13);
    line.sd_layout.heightIs(0.5).leftSpaceToView(self.contentView, 0).rightSpaceToView(self.contentView, 0).topSpaceToView(_deviceName, 12);
    _personLiableTitle.sd_layout.heightIs(14).widthIs(84).leftSpaceToView(self.contentView, 12).topSpaceToView(line, 17);
    _personLiable.sd_layout.heightIs(14).leftSpaceToView(_personLiableTitle, 0).rightSpaceToView(self.contentView, 40)
    .topSpaceToView(line, 17);
    _phoneImage.sd_layout.heightIs(24).widthIs(24).rightSpaceToView(self.contentView, 12).topSpaceToView(line, 12);
    _titleView.sd_layout.heightIs(34).leftSpaceToView(self.contentView, 8).rightSpaceToView(self.contentView,8)
    .topSpaceToView(_personLiableTitle, 17);
    _tableView.sd_layout.leftSpaceToView(self.contentView,  8).rightSpaceToView(self.contentView, 8).topSpaceToView(_titleView, 0);
//    _tableUIView.sd_layout.leftSpaceToView(self.contentView,  8).rightSpaceToView(self.contentView, 24).topSpaceToView(_titleView, 0);
//    _tableView.backgroundColor = [UIColor redColor];
//    self.contentView.backgroundColor = [UIColor blueColor];
    [self setupAutoHeightWithBottomView:_tableView bottomMargin:0];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_dataArray){
        if(_dataArray.count > 0){
            return _dataArray.count;
        }
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReportAlarmPointListCell *cell = [tableView dequeueReusableCellWithIdentifier:[@"ReportAlarmPointListCell" description]];
//    ReportAlarmPointListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportAlarmPointListCell" forIndexPath:indexPath];
    if(!cell){
        cell = [[ReportAlarmPointListCell alloc]init];
        //禁止点击
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
//    [cell layoutIfNeeded];
    
    [cell setDataModel:_dataArray[indexPath.row]];
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    float h = [tableView cellHeightForIndexPath:indexPath cellContentViewWidth:self.tableView.frame.size.width tableView:tableView];
    if(_tableviewH == 0){
        _tableviewH = _dataArray.count * h;
        _tableView.sd_layout.leftSpaceToView(self.contentView,  8).rightSpaceToView(self.contentView, 8).topSpaceToView(_titleView, 0).heightIs(_tableviewH);
    }
    return h;
}


//- (void)layoutSubviews {
//    [_tableView sd_clearAutoLayoutSettings];
//    NSLog(@"dataArray长度：%lu高度%f",_dataArray.count,_cellH);
//    _tableView.sd_layout.leftSpaceToView(self.contentView,  8).rightSpaceToView(self.contentView, 12).topSpaceToView(_titleView, 0).heightIs(_cellH  * _dataArray.count+ 20);
//
//     [self setupAutoHeightWithBottomView:_tableView bottomMargin:8];
//     [super layoutSubviews];
//
//}



-(void)setDataModel:(ReportDeviceModel *)data arrayIndex:(NSInteger)index{
    if(data){
        _deviceName.text = data.efairyproject_name;
        _score.text = [NSString stringWithFormat:@"评分%lu",data.efairyproject_security_score];
        if(data.efairyproject_security_score >= 80 && data.efairyproject_security_score <100){
            _score.textColor = ColorFromRGB(0xff7200);//橙色
        }else if(data.efairyproject_security_score == 100){
             _score.textColor = ColorFromRGB(0x3FB059);//绿色
        }else{
             _score.textColor = ColorFromRGB(0xff0000);//红色
        }
        _personLiable.text = data.efairyproject_user_name;
        if(data.efairyproject_device_statistics){
            _dataArray =data.efairyproject_device_statistics.device_statistics_list;
        }
        _phoneImage.tag = index;
        [self.tableView reloadData];
        
    }
}

- (float) heightForString:(NSString *)value andWidth:(float)width{
    /**
     5.根据text的font和字符串自动算出size（重点）
     200:你希望的最大宽度
     MAXFLOAT:最大高度为最大浮点数
     **/
    
    
    CGSize size = [value boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f]}
                                          context:nil].size;
    
    

    return size.height + 16.0;
 
}



-(void)onClickPhoneImage:(UIGestureRecognizer *)tap{
//    UIImageView *imageView = tap.
    __weak typeof(self) weakSelf = self;
    if(weakSelf.callPhoneBlock){
        weakSelf.callPhoneBlock(tap.view.tag);
    }
}


@end
